﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Backend.JYD.EFDAL.DAL
{
    public class BidRecordDAL : IDAL.Bid.IBidRecord
    {
        #region IBidRecord 成员

        public long AddBidRecord(Model.Bid.BidRecord record)
        {
            using (JydDataContext db = new JydDataContext())
            {
                db.BidRecords.Add(record);
                db.SaveChanges();
                return record.BidId;
            }
        }

        public bool UpdateBidRecord(Model.Bid.BidRecord record)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.BidRecords.Where(p => p.BidId == record.BidId);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.UpdateTime = DateTime.Now;
                    temp.LoanMoney = record.LoanMoney;
                    temp.Rate = record.Rate;
                    temp.Term = record.Term;
                    //....
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }

        public bool CancelBidRecord(long bidId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.BidRecords.Where(p => p.BidId == bidId);
                var temp = query.FirstOrDefault();
                if (temp != null)
                {
                    temp.UpdateTime = DateTime.Now;
                    if (temp.Status <= 1)
                    {
                        if (temp.Status == 1)
                        {
                            //冻结资金需要解冻
                        }
                        temp.Status = -1;
                        return db.SaveChanges() > 0;
                    }
                }
            }
            return false;
        }

        public Model.Bid.BidRecord GetdBidRecordByBidId(long bidId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.BidRecords.Where(p => p.BidId == bidId);
                return query.FirstOrDefault();
            }
        }

        public IList<Model.Bid.BidRecord> GetdBidRecordsByInviteId(long inviteId)
        {
            using (JydDataContext db = new JydDataContext())
            {
                var query = db.BidRecords.Where(p => p.InviteId == inviteId && p.Status > -1);
                return query.ToList();
            }
        }

        #endregion
    }
}
